package doc

import (
	"fmt"
	"github.com/phpdi/ant/autodoc"
	"runtime"
	"strings"
)
var autoDoc *autodoc.AutoDoc

func init()  {

	autoDoc = autodoc.New(nil,nil).SetMethod("POST")
}


func DefaultAuto(req interface{}, ack interface{}, requireFields ...string) *autodoc.AutoDoc {
	var (
		c string //控制器
		m string //方法
	)
	c, m = getUrl()
	apiName := getApiName(c, m)

	auto := autodoc.New(req, ack).
		Require(requireFields...).
		SetMethod("POST").
		SetUrl(fmt.Sprintf("%s/%s", strings.ToLower(strings.Replace(c, "Controller", "", 1)), strings.ToLower(m))).
		SetTitle(apiName)

	return auto
}

func getUrl() (c string, m string) {
	pc, _, _, _ := runtime.Caller(2)
	a := runtime.FuncForPC(pc).Name()

	a=strings.TrimLeft(a,"Test")

	arr1 := strings.Split(a, "_")

	return arr1[1], arr1[2]

}


func getApiName(c string, m string) string {
	p := autodoc.NewParseFile()

	//beego框架控制器
	p.Config.ScanDir = "/home/yu/code/yungengxin2019/lwyapi/controllers"


	res, err := p.Do()
	if err != nil {

	}

	for _, v := range res {
		if v.Method == fmt.Sprintf("%sController_%s", c, m) {
			return v.Remark
		}
	}
	return ""
}
